<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/21
 * Time: 10:09
 * 发现
 */
namespace app\api\controller;
use think\Db;
class Home extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        header("Access-Control-Allow-Origin:*");
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 2111){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }
    }

    /**
     * 获取轮播图：getBanner（只有首页有，所以此处可忽略）
     */
//    public function getBanner(){
//        extract(input());
//        $data = Db::name('lunbo')
//            ->where('lunbo_id','>',0)
//            ->field(['lunbo_id','lunbo_image','lunbo_type','lunbo_who_id'])
//            ->order('lunbo_time DESC')
//            ->limit (5)
//            ->select();
//        return json(['status'=>200,'msg'=>$data]);
//    }

    /**
     * 获取轮播图内容：getBannerContent
     */
    public function getBannerContent(){
        extract(input());
        $data = Db::table('up_banner')
            ->where(['banner_id'=>$lunbo_id])
            ->find();
        if($data['banner_type'] == 4){
            $data['video_url'] = Db::table('up_video')
                ->where(['video_id'=>$data['banner_type_id']])
                ->value('video_url');
        }
        return json(['status'=>200,'msg'=>$data]);
    }

    /***
     * $user_id
     * @return \think\response\Json
     * 获取首页数据
     */
    public function getHomeData()
    {
        extract(input());
        // 1、轮播图数据
        $lb = Db::table('up_banner')
            ->where('banner_id','>',0)
            ->where('banner_status',1)
            ->field(['banner_id','banner_img','banner_type','banner_type_id','banner_link'])
            ->order('banner_time ASC')
            ->limit (5)
            ->select();
        // 2、学生社团（军事类社团）
        $upids = Db::table('up_assn')
            ->where('assn_type',1)
            ->where('assn_status',1)
            ->column('assn_id');
        $ids1 = array();
        if(count($upids) > 4){
            $keys = array_rand($upids,4);
            foreach ($keys as $v){
                $ids1[] = $upids[$v];
            }
        }else{
            $ids1 = $upids;
        }
        $up = Db::table('up_assn')->alias('a')
            ->join ('up_school s','a.assn_school = s.sc_id','LEFT')
            ->where('assn_type',1)
            ->where('assn_status',1)
            ->where('assn_id','in',$ids1)
//            ->where('assn_sheng', $assn_sheng) //放开省市筛选的话打开这个判断即可
            ->field('assn_id,assn_name,assn_image,sc_name,assn_school_qu,assn_address,assn_code')
            ->order('assn_time ASC')
            ->page(1,4)
            ->select();
        // 3、非军事类社团
        $upids = Db::table('up_assn')
            ->where('assn_type',2)
            ->where('assn_status',1)
            ->column('assn_id');
        $ids1 = array();
        if(count($upids) > 4){
            $keys = array_rand($upids,4);
            foreach ($keys as $v){
                $ids1[] = $upids[$v];
            }
        }else{
            $ids1 = $upids;
        }
        $down = Db::table('up_assn')->alias('a')
            ->join ('up_school s','a.assn_school = s.sc_id','LEFT')
            ->where('assn_type',2)
            ->where('assn_status',1)
            ->where('assn_id','in',$ids1)
//            ->where('assn_sheng', $assn_sheng) //放开省市筛选的话打开这个判断即可
            ->field('assn_id,assn_name,assn_image,sc_name,assn_school_qu,assn_address,assn_type,assn_code')
            ->order('assn_time ASC')
            ->page(1,4)
            ->select();
        $myall = Db::table('up_attention_assn')
            ->where('att_user_id',$user_id)
            ->column('att_assn_id');
        if($up){
            foreach ($up as $k=>$v){
                $up[$k]['guanzhu'] = 1; // 未关注
                if(in_array($v['assn_id'],$myall)){
                    $up[$k]['guanzhu'] = 2; // 已关注
                }
            }
        }
        if($down){
            foreach ($down as $k=>$v){
                $down[$k]['guanzhu'] = 1; // 未关注
                if(in_array($v['assn_id'],$myall)){
                    $down[$k]['guanzhu'] = 2; // 已关注
                }
            }
        }
        return json(['status'=>200,'lb'=>$lb,'up'=>$up,'down'=>$down]);
    }
    /***
     * $user_id
     * @return \think\response\Json
     * 获取所有社团数据
     * type,page
     */
    public function getAllAssnData()
    {
        extract(input());
        $where['assn_type'] = $type;
        $where['assn_status'] = 1;
        //放开省市筛选的话打开这个判断即可
        if($sheng != ''){
            $where['assn_sheng'] = $sheng;
        }
        if($shi != ''){
            $where['assn_shi'] = $shi;
        }
        $up = Db::table('up_assn')->alias('a')
            ->join ('up_school s','a.assn_school = s.sc_id','LEFT')
            ->where($where)
            ->field('assn_id,assn_name,assn_image,sc_name,assn_school_qu,assn_address, assn_code')
            ->order('assn_time ASC')
            ->page($page,15)
            ->select();
        $myall = Db::table('up_attention_assn')
            ->where('att_user_id',$user_id)
            ->column('att_assn_id');
        if($up){
            foreach ($up as $k=>$v){
                $up[$k]['guanzhu'] = 1; // 未关注
                if(in_array($v['assn_id'],$myall)){
                    $up[$k]['guanzhu'] = 2; // 已关注
                }
            }
        }
        return json(['status'=>200,'msg'=>$up]);
    }
    /***
     * assn_id
     * @return \think\response\Json
     * 获取某个社团详情
     */
    public function getAssnInfo(){
        extract(input());
        $field = 'assn_id,assn_name,assn_image,assn_school_qu,assn_teacher,assn_company,assn_address, assn_code';
        $field .= ',assn_notice,assn_update_time';
        $field .= ',assn_number,sc_name';
        $data = Db::table('up_assn')->alias('a')
            ->join ('up_school s','a.assn_school = s.sc_id','LEFT')
            ->where('assn_id',$assn_id)
            ->field($field)
            ->find();
        $member = Db::table('up_assn_member')->alias('m')
            ->join ('up_u_user u','m.am_user_id = u.user_id','LEFT')
            ->where('am_assn_id',$assn_id)
            ->where('am_status',2)
            ->order('am_user_type DESC')
            ->field('user_id,user_username,user_image')
            ->page(1,5)
            ->select();
        //可不可以申请成为管理员
        $i = Db::table('up_assn_member')
            ->where('am_user_id',$user_id)
            ->where('am_assn_id',$assn_id)
            ->where('am_user_type',1)
            ->where('am_status',2)
            ->find();
        if($i){
            $auth_data = Db::table('up_notice_assn')
                ->where('n_apply_user_id',$user_id)
                ->where('n_assn_id',$assn_id)
                ->where('n_auth',2)
                ->find();
            $auth = 0;
            if($auth_data){
                $auth = $auth_data['n_audit_status'];
            }
        }else{
            $auth = 100;
        }
        return json(array('status' => 200, 'msg' => $data, 'member' => $member, 'auth' => $auth));
    }

    /**
     * 社团关注和取消关注
     * @return \think\response\Json
     */
    public function assnLikeNo()
    {
        extract(input());
        $have = Db::table('up_attention_assn')
            ->where('att_user_id',$user_id)
            ->where('att_assn_id',$assn_id)
            ->find();
        if($have){
            $res = Db::table('up_attention_assn')
                ->where('att_user_id',$user_id)
                ->where('att_assn_id',$assn_id)
                ->delete();
        }else{
            $res = Db::table('up_attention_assn')->insert([
                'att_user_id' => $user_id,
                'att_assn_id' => $assn_id,
                'att_time' => date('Y-m-d H:i:s')
            ]);
        }
        if($res){
            return json(array('status' => 200, 'msg' => '操作成功'));
        }else{
            return json(array('status' => 100, 'msg' => '操作失败'));
        }
    }
    /**
     * 社团招新
     * @return \think\response\Json
     */
    public function assnJoin()
    {
        extract(input());
        $data = Db::table('up_assn')
            ->where('assn_id',$assn_id)
            ->field('assn_join,assn_type')
            ->find();
        $member = Db::table('up_assn_member')
            ->where('am_user_id',$user_id)
            ->where('am_assn_id',$assn_id)
            ->find();
        if(!$member){
            $status = 0; // 0是未申请  1是申请中  2是同意  3是拒绝
        }else{
            $status = $member['am_status'];
        }
        if($data['assn_type'] == 1){
            $s = Db::table('up_assn_member')->alias('m')
                ->join('up_assn a','m.am_assn_id=a.assn_id','LEFT')
                ->where('am_user_id',$user_id)
                ->where('am_status','in','1,2')
                ->where('assn_type',1)
                ->find();
            if($s && $status == 0){
                $status = 4;
            }
        }

        return json(array('status' => 200, 'msg' => $data, 'join_status' => $status));
    }
    /**
     * 社团招新——加入
     * @return \think\response\Json
     * $assn_id
     */
    public function userJoinAssn()
    {
        extract(input());
        $member = Db::table('up_assn_member')
            ->where('am_user_id',$user_id)
            ->where('am_assn_id',$assn_id)
            ->find();
        //这个查询只是为了以防万一
        $have = Db::table('up_notice_assn')
            ->where('n_apply_user_id',$user_id)
            ->where('n_assn_id',$assn_id)
            ->where('n_auth',1)
            ->find();
            
        $today = date('Y-m-d H:i:s');
        if(!$member){
            // 1、申请加入
            $insert = [
                'am_assn_id'=>$assn_id,
                'am_user_id'=>$user_id,
                'am_user_type'=>1,
                'am_time'=>$today,
                'am_status'=>1
            ];
            Db::table('up_assn_member')->insert($insert);
            // 2、入消息表
        }else{
            if($member['am_status'] == 1){
                return json(array('status' => 100, 'msg' => '申请中！'));
            }
            if($member['am_status'] == 2){
                return json(array('status' => 100, 'msg' => '您已经是此社团成员了！'));
            }
            // 1、修改（拒绝后再次申请）
            $update = [
                'am_time'=>$today,
                'am_status'=>1
            ];
            Db::table('up_assn_member')
                ->where('am_user_id',$user_id)
                ->where('am_assn_id',$assn_id)
                ->update($update);
        }
        //2、判断一下have，以防万一，入专用的社团消息
        if($have){
            $update2 = [
                'n_del_user_id'=>',0,',
                'n_apply_time'=>$today,
                'n_audit_status'=>1
            ];
            Db::table('up_notice_assn')
                ->where('n_apply_user_id',$user_id)
                ->where('n_assn_id',$assn_id)
                ->where('n_auth',1)
                ->update($update2);
        }else{
            $n_assn_manage = Db::table('up_assn_member')
                ->where('am_assn_id',$assn_id)
                ->where('am_user_type',2)
                ->where('am_status',2)
                ->column('am_user_id');
            $n_assn_manage = implode($n_assn_manage,',');
            $insert2 = [
                'n_assn_id'=>$assn_id,
                'n_assn_manage'=>',' . $n_assn_manage . ',',
                'n_apply_user_id'=>$user_id,
                'n_del_user_id'=>',0,',
                'n_apply_time'=>$today,
                'n_audit_status'=>1
            ];
            Db::table('up_notice_assn')->insert($insert2);
        }
    
        // 成员信息
        $staff_insert = [
            'staff_type' => 2,
            'staff_soldier_type' => $assn_type, // 社团类型   1 军事类  2 非军事类
            'staff_school_id' => $assn_school, // 学校ID
            'staff_name' => $staff_name, // 手动填写的姓名
            'staff_gender' => $staff_gender, // 性别  1 男  2 女
            'staff_birthday' => $staff_birthday, // 出生年月
            'staff_faculty' => $staff_faculty, // 所在院系
            'staff_specilay' => $staff_specilay, // 特长
            'staff_phone' => $staff_phone, // 联系电话
            'staff_insert_time' => date('Y-m-d H:i:s'), // 记录插入时间
            'staff_user_id' => $user_id,
            'staff_assn_id' => $assn_id, // 社团ID
        ];
        if($assn_type == 1){ // 军事类
            $staff_insert['staff_assn_name'] = $staff_assn_name; // 社团名称
            $staff_insert['staff_user_card'] = $staff_user_card; // 身份证号
            $staff_insert['staff_politics'] = $staff_politics; // 政治面貌
            $staff_insert['staff_assn_duty'] = $staff_assn_duty; // 社团职务
            $staff_insert['staff_in_assn_time'] = $staff_in_assn_time; // 加入社团时间
            $staff_insert['staff_old_troops'] = $staff_old_troops; // 原服役部队
            $staff_insert['staff_sign'] = $staff_sign; // 手写签名  图片
            $staff_insert['staff_sign_time'] = $staff_sign_time; // 签名时间
            
            $is_have = Db::table('up_staff')
                ->where('staff_user_id', $user_id)
                ->where('staff_soldier_type', 1)
                ->where('staff_type', 2)
                ->find();
            // 如果有记录，则删除
            if($is_have){
                Db::table('up_staff')
                    ->where('staff_user_id', $user_id)
                    ->where('staff_soldier_type', 1)
                    ->where('staff_type', 2)
                    ->delete();
            }
            Db::table('up_staff')->insert($staff_insert);
        }else{ // 非军事类
            $staff_insert['staff_grade'] = $staff_grade; // 年级
            
            $is_have = Db::table('up_staff')
                ->where('staff_user_id', $user_id)
                ->where('staff_assn_id', $assn_id)
                ->where('staff_soldier_type', 2)
                ->where('staff_type', 2)
                ->find();
            if($is_have){
                Db::table('up_staff')
                    ->where('staff_user_id', $user_id)
                    ->where('staff_assn_id', $assn_id)
                    ->where('staff_soldier_type', 2)
                    ->where('staff_type', 2)
                    ->delete();
            }
            Db::table('up_staff')->insert($staff_insert);
        }
        
        //=========jpush
        //社团名称
        $assn_name = Db::table('up_assn')->where('assn_id', $assn_id)->value('assn_name');
        $str = '有用户申请加入' . $assn_name . '社团';
        //推送群体
        $send_id = Db::table('up_assn_member')
            ->where('am_assn_id', $assn_id)
            ->where('am_user_type', 2)
            ->column('am_user_id');
        $list = [];//需要推送的用户数组
        foreach($send_id as $k => $v){
            $val = (string)$v;
            array_push($list, $val);
        }
        try{
            $this->Jpush(1, $list, $str, ['send_type' => 10]);
        }catch(\Exception $e){
        }
        
        return json(array('status' => 200, 'msg' => '已申请，请等待审核！'));
    }
    /**
     * 社团成员升级——申请成为管理员
     * @return \think\response\Json
     * $assn_id
     */
    public function userLevelAssn()
    {
        extract(input());
        $member = Db::table('up_assn_member')
            ->where('am_user_id',$user_id)
            ->where('am_assn_id',$assn_id)
            ->find();
        //这个查询只是为了以防万一
        $have = Db::table('up_notice_assn')
            ->where('n_apply_user_id',$user_id)
            ->where('n_assn_id',$assn_id)
            ->where('n_auth',2)
            ->find();
        $today = date('Y-m-d H:i:s');
        if(!$member){
            return json(array('status' => 100, 'msg' => '您还不是此社团成员！'));
            // 2、入消息表
        }else{
            if($member['am_status'] != 2){
                return json(array('status' => 100, 'msg' => '您还不是此社团成员！'));
            }
            if($member['am_user_type'] == 2){
                return json(array('status' => 100, 'msg' => '您已经是此社团管理员了！'));
            }
        }
        //2、判断一下have，以防万一，入专用的社团消息
        if($have){
            $update2 = [
                'n_del_user_id'=>',0,',
                'n_apply_time'=>$today,
                'n_audit_status'=>1
            ];
            Db::table('up_notice_assn')
                ->where('n_apply_user_id',$user_id)
                ->where('n_assn_id',$assn_id)
                ->where('n_auth',2)
                ->update($update2);
        }else{
            $n_assn_manage = Db::table('up_assn_member')
                ->where('am_assn_id',$assn_id)
                ->where('am_user_type',2)
                ->where('am_status',2)
                ->column('am_user_id');
            $n_assn_manage = implode($n_assn_manage,',');
            $insert2 = [
                'n_assn_id'=>$assn_id,
                'n_assn_manage'=>',' . $n_assn_manage . ',',
                'n_apply_user_id'=>$user_id,
                'n_del_user_id'=>',0,',
                'n_apply_time'=>$today,
                'n_audit_status'=>1,
                'n_auth'=>2
            ];
            Db::table('up_notice_assn')->insert($insert2);
        }
        //=========jpush
        //社团名称
        $assn_name = Db::table('up_assn')->where('assn_id', $assn_id)->value('assn_name');
        $str = '有用户申请成为' . $assn_name . '社团的管理员';
        //推送群体
        $send_id = Db::table('up_assn_member')
            ->where('am_assn_id', $assn_id)
            ->where('am_user_type', 2)
            ->column('am_user_id');
        $list = [];//需要推送的用户数组
        foreach($send_id as $k => $v){
            $val = (string)$v;
            array_push($list, $val);
        }
        try{
            $this->Jpush(1, $list, $str, ['send_type' => 10]);
        }catch(\Exception $e){
        }

        return json(array('status' => 200, 'msg' => '已申请，请等待审核！'));
    }

    /***
     * type 1是文件 2是视频
     * assn_id 社团id
     * page 页码
     * 社团文件
     */
    public function assnFilesVideos(){
        extract(input());
        $where = [
            'af_type' => $type,
            'af_assn_id' => $assn_id
        ];
        if($type == 2){
            $is = Db::table('up_assn_member')
                ->where('am_user_id',$user_id)
                ->where('am_assn_id',$assn_id)
                ->where('am_status',2)
                ->find();
            if(!$is){
                $where['af_status'] = 1;
            }
        }
        $files = Db::table('up_assn_files')
            ->where($where)
            ->page($page,15)
            ->select();
        $can_do = $this->isAssnLeader($user_id,$assn_id);
        return json(array('status' => 200, 'msg' => $files, 'can_do' => $can_do));
    }
    /***
     * type 1是文件 2是视频
     * assn_id 社团id
     * $url 链接地址
     * $type 文件类型（文件和视频）
     * $name $size（文件名字和文件大小看咋传，待定）
     * $status 可见范围
     * 上传文件或者视频
     */
    public function uploadFileOrVideo(){
        extract(input());
        $can = $this->isAssnLeader($user_id,$assn_id);
        if($can == 0){
            return json(array('status' => 100, 'msg' => '您不是此社团管理员！'));
        }else{
            if(!isset($exe)){
                $exe = 'mp4';
            }
//            $af_exe = explode('.',$url)[1];
            $data = [
                'af_assn_id' => $assn_id,
                'af_assn_url' => $url,
                'af_user_id' => $user_id,
                'af_type' => $type,
                'af_name' => $name,
                'af_size' => $size,
                'af_exe' => $exe,
                'af_time' => date('Y-m-d H:i:s'),
                'af_status' => $status
            ];
            $res = Db::table('up_assn_files')->insert($data);
            if($res){
                return json(array('status' => 200, 'msg' => '上传成功'));
            }else{
                return json(array('status' => 100, 'msg' => '上传失败'));
            }
        }
    }

    /***
     * $af_id  文件id，逗号隔开
     * 删除文件或者视频
     */
    public function delFileOrVideo(){
        extract(input());
        $af_id = trim($af_id,',');
        $file = Db::table('up_assn_files')
            ->where('af_id','in',$af_id)
            ->field('af_assn_id')
            ->find();
        $can = $this->isAssnLeader($user_id,$file['af_assn_id']);
        if($can == 1){
            $res = Db::table('up_assn_files')
                ->where('af_id','in',$af_id)
                ->delete();
            if($res){
                return json(array('status' => 200, 'msg' => '删除成功'));
            }else{
                return json(array('status' => 100, 'msg' => '删除失败'));
            }
        }else{
            return json(array('status' => 100, 'msg' => '您不是此社团管理员！'));
        }
    }
    /***
     * $assn_id 社团id
     * 获得此社团的成员
     */
    public function getAssnMembers(){
        extract(input());
        $online = Db::table('up_assn_member')->alias('am')
            ->join ('up_u_user u','am.am_user_id = u.user_id','LEFT')
            ->where('am_assn_id',$assn_id)
            ->where('am_status',2)
//            ->where('user_status',1)
            ->where('user_online',1)
            ->field('user_id,user_username,user_image,am_user_type,am_id')
            ->order('am_user_type DESC')
            ->select();
        $downline = Db::table('up_assn_member')->alias('am')
            ->join ('up_u_user u','am.am_user_id = u.user_id','LEFT')
            ->where('am_assn_id',$assn_id)
            ->where('am_status',2)
//            ->where('user_status',1)
            ->where('user_online',2)
            ->field('user_id,user_username,user_image,am_user_type,am_id')
            ->order('am_user_type DESC')
            ->select();
        $can = $this->isAssnLeader($user_id,$assn_id);
        return json(array('status' => 200, 'online' => $online, 'downline' => $downline, 'online_num' => count($online), 'downline_num' => count($downline),'can'=>$can));
    }
    /***
     * 删除社团成员
     * 获得此社团的成员
     */
    public function delAssnMembers(){
        extract(input());
        $info = Db::table('up_assn_member')
            ->where('am_id',$am_id)
            ->find();
        $can = $this->isAssnLeader($user_id,$info['am_assn_id']);
        if($can == 1 && $info['am_user_type'] == 1){
            Db::table('up_assn')
                ->where('assn_id',$info['am_assn_id'])
                ->setDec('assn_number',1);
            Db::table('up_assn_member')
                ->where('am_id',$am_id)
                ->delete();
            Db::table('up_notice_assn')
                ->where('n_assn_id',$info['am_assn_id'])
                ->where('n_apply_user_id',$info['am_user_id'])
                ->delete();
            return json(array('status' => 200,'msg'=>'删除成功'));
        }else{
            return json(array('status' => 100,'msg'=>'您无权删除此人'));
        }
    }

    /**
     * 社团公告——查看
     * @return \think\response\Json
     */
    public function assnNotice()
    {
        extract(input());
        $data = Db::table('up_assn')
            ->where('assn_id',$assn_id)
            ->field('assn_notice')
            ->find();
        $can = $this->isAssnLeader($user_id,$assn_id);
        return json(array('status' => 200, 'msg' => $data, 'can' => $can));
    }
    /**
     * 社团公告——编辑
     * @return \think\response\Json
     */
    public function assnNoticeEdit()
    {
        extract(input());
        $can = $this->isAssnLeader($user_id,$assn_id);
        if($can == 1){
            Db::table('up_assn')
                ->where('assn_id',$assn_id)
                ->update(['assn_notice'=>$assn_notice,'assn_update_time'=>date('Y-m-d H:i:s')]);
            return json(array('status' => 200, 'msg' => '修改成功！'));
        }else{
            return json(array('status' => 100, 'msg' => '您无权修改！'));
        }
    }
    
    /**
     * 选择学校时，判断该学校是否有军事社团或军事类社团申请
     * @return \think\response\Json
     */
    public function isHave(){
        $staff_school_id = input('staff_school_id'); // 学校
        $user_id = input('user_id'); // 学校
        //=========== 该用户有没有过社团创建申请
        $is_have = Db::table('up_staff')
            ->where('staff_user_id', $user_id)
            ->where('staff_type', 1)
            ->where('staff_soldier_type', 1)
            ->find();
        // 如果有记录，则为编辑
        if($is_have){
            //判断除了该条记录外，该学校有无其他军事类申请
                $is_have_other = Db::table('up_staff')
                    ->where('staff_soldier_type', 1)
                    ->where('staff_type', 1)
                    ->where('staff_status', 'NEQ', 3)
                    ->where('staff_school_id', $staff_school_id)
//                    ->where('staff_user_id', $user_id)
                    ->where('staff_id', 'NEQ', $is_have['staff_id'])
                    ->find();
                if($is_have_other){
                    $is_have_assn = Db::table('up_assn')
                        ->where('assn_school', $staff_school_id)
                        ->where('assn_type', 1)
                        ->find();
                    if($is_have_assn){
                        return json(['status' => 401, 'msg' => '您当前所在的学校已有社团，您可以申请加入社团', 'assn_id' => $is_have_assn['assn_id']]);
                    }else{
                        return json(['status' => 402, 'msg' => '您当前所在的学校已有社团申请']);
                    }
                }else{
                    return json(['status' => 200, 'msg' => true]);
                }
        }else{ // 如果没有，则为新申请记录
            // 则判断该学校有没有其他军事社团
            $is_have_other = Db::table('up_staff')
                    ->where('staff_soldier_type', 1)
                    ->where('staff_type', 1)
                    ->where('staff_status', 'NEQ', 3)
                    ->where('staff_school_id', $staff_school_id)
//                    ->where('staff_user_id', $user_id)
                    ->find();
            if($is_have_other){
                $is_have_assn = Db::table('up_assn')
                    ->where('assn_school', $staff_school_id)
                    ->where('assn_type', 1)
                    ->find();
                if($is_have_assn){
                    return json(['status' => 401, 'msg' => '您当前所在的学校已有社团，您可以申请加入社团', 'assn_id' => $is_have_assn['assn_id']]);
                }else{
                    return json(['status' => 402, 'msg' => '您当前所在的学校已有社团申请']);
                }
            }else{
                return json(['status' => 200, 'msg' => true]);
            }
        }
    }
    
    /**
     * 提交/编辑 国防/非国防军事类社团创建申请表
     * @return \think\response\Json
     */
    public function instructor(){
        extract(input());
        $staff_soldier_type = input('staff_soldier_type'); // 1 军事类    2 非军事类
        $staff_school_id = input('staff_school_id'); // 学校
        $staff_assn_name = input('staff_assn_name'); // 社团名称
        $staff_assn_create_time = input('staff_assn_create_time'); // 社团成立时间
        $staff_assn_address = input('staff_assn_address'); // 社团联系地址
        $staff_assn_up = input('staff_assn_up'); // 社团指导单位
        $staff_assn_teacher = input('staff_assn_teacher'); // 社团指导老师
        $staff_assn_logo = input('staff_assn_logo'); // 社团logo
        $staff_assn_intro = input('staff_assn_intro'); // 社团简介
        $staff_retire_people = input('staff_retire_people'); // 退伍军人数量
        $staff_other_people = input('staff_other_people'); // 其他人员数量
        $staff_activity = input('staff_activity'); // 开展活动
        $staff_assn_duty = input('staff_assn_duty'); // 社团职务
        $staff_name = input('staff_name'); // 申请人姓名
        $staff_gender = input('staff_gender'); // 申请人性别   1 男  2 女
        $staff_faculty = input('staff_faculty'); // 院系
        $staff_old_troops = input('staff_old_troops'); // 原服役部队
        $staff_specilay = input('staff_specilay'); // 军事专业特长
        $staff_politics = input('staff_politics'); // 政治面貌
        $staff_phone = input('staff_phone'); // 申请人联系电话
        $staff_student_card = input('staff_student_card'); // 学生证照片
//        $staff_front_img = input('staff_front_img'); // 身份证正面照
//        $staff_back_img = input('staff_back_img'); // 身份证反面照
        $staff_user_card = input('staff_user_card'); // 身份证反面照
        $staff_sign = input('staff_sign'); // 申请人手写签名--图片
        $staff_sign_time = input('staff_sign_time'); // 签名时间
        $staff_birthday = input('staff_birthday'); // 出生年月
        $staff_grade = input('staff_grade'); // 年级

//        $is_first = input('is_first'); // 是否是提交申请   1 提交   2 编辑
//        $type = input('type'); // 1 创建社团申请表   2 加入社团申请表
//        $staff_in_assn_time = input('staff_in_assn_time'); // 入社时间
//        $staff_coach_code = input('staff_coach_code'); // 教练员代码
//        $staff_address = input('staff_address'); // 联系地址
//        $staff_intro = input('staff_intro'); // 个人简历
//        $staff_new_major = input('staff_new_major'); // 现在校专业
//        $staff_my_assn_time = input('staff_my_assn_time'); // 担任本社团负责人时间
//        $staff_assn_sign = input('staff_assn_sign'); // 社团负责人签字--图片
//        $staff_user_card = input('staff_user_card'); // 教员身份证号
        
        //=========== 该用户有没有过社团创建申请
        $is_have = Db::table('up_staff')
            ->where('staff_user_id', $user_id)
            ->where('staff_type', 1)
            ->where('staff_soldier_type', $staff_soldier_type)
            ->find();
        // 如果有记录，则为编辑
        if($is_have){
            //如果是军事类，判断除了该条记录外，该学校有无其他军事类申请
            if($staff_soldier_type == 1){
                $is_have_other = Db::table('up_staff')
                    ->where('staff_soldier_type', 1)
                    ->where('staff_type', 1)
                    ->where('staff_status', 'NEQ', 3)
                    ->where('staff_school_id', $staff_school_id)
//                    ->where('staff_user_id', $user_id)
                    ->where('staff_id', 'NEQ', $is_have['staff_id'])
                    ->find();
                if($is_have_other){
                    return json(['status' => 401, 'msg' => '您当前所在的学校已有社团，您可以申请加入社团']);
                }
            }
            // 判断该用户除了本条记录外，有没有其他同类型社团申请
            $is_have_user = Db::table('up_staff')
                ->where('staff_type', 1)
//                    ->where('staff_status', 'NEQ', 3)
                ->where('staff_id', 'NEQ', $is_have['staff_id'])
                ->where('staff_soldier_type', $staff_soldier_type)
                ->where('staff_user_id', $user_id)
                ->find();
            if($is_have_user){
                return json(['status' => 0, 'msg' => '您已经申请过同类型社团']);
            }
        }else{ // 如果没有，则为新申请记录
            // 如果是军事社团，则判断该学校有没有其他军事社团
            if($staff_soldier_type == 1){
                $is_have_school = Db::table('up_staff')
                    ->where('staff_soldier_type', 1)
                    ->where('staff_type', 1)
                    ->where('staff_status', 'NEQ', 3)
                    ->where('staff_school_id', $staff_school_id)
//                    ->where('staff_user_id', $user_id)
                    ->find();
                if($is_have_school){
                    return json(['status' => 401, 'msg' => '您当前所在的学校已有社团，您可以申请加入社团']);
                }
            }
            
        }
        
        //============== 所申请学校有无其他军事类社团
//        if($staff_soldier_type == 1){
//            // 如果有，则为编辑，需要判断除了该用户外，该学校有无其他用户申请的记录
//            if($is_have){
//                $is_have_other = Db::table('up_staff')
//                    ->where('staff_soldier_type', 1)
//                    ->where('staff_type', 1)
//                    ->where('staff_status', 'NEQ', 3)
//                    ->where('staff_school_id', $staff_school_id)
////                    ->where('staff_user_id', $user_id)
//                    ->where('staff_id', 'NEQ', $is_have['staff_id'])
//                    ->find();
//                if($is_have_other){
//                    return json(['status' => 0, 'msg' => '该学校已有同类型的军事类社团']);
//                }
//            }else{
//                // 该学校有没有同类社团
//                $is_have_school = Db::table('up_staff')
//                    ->where('staff_soldier_type', 1)
//                    ->where('staff_type', 1)
//                    ->where('staff_status', 'NEQ', 3)
//                    ->where('staff_school_id', $staff_school_id)
////                    ->where('staff_user_id', $user_id)
//                    ->find();
//                if($is_have_school){
//                    return json(['status' => 0, 'msg' => '该学校已有同类型的军事类社团']);
//                }
//                // 申请人是否创建国军事类社团
//                $is_have_user = Db::table('up_staff')
//                    ->where('staff_type', 1)
////                    ->where('staff_status', 'NEQ', 3)
//                    ->where('staff_soldier_type', 1)
//                    ->where('staff_user_id', $user_id)
//                    ->find();
//                if($is_have_user){
//                    return json(['status' => 0, 'msg' => '您已经申请过军事类社团']);
//                }
//            }
//        }
        
        $data = [
            'staff_school_id' => $staff_school_id,
            'staff_assn_name' => $staff_assn_name,
            'staff_assn_address' => $staff_assn_address,
            'staff_assn_up' => $staff_assn_up,
            'staff_assn_teacher' => $staff_assn_teacher,
            'staff_assn_logo' => $staff_assn_logo,
            'staff_assn_intro' => $staff_assn_intro,
            'staff_name' => $staff_name,
            'staff_gender' => $staff_gender,
            'staff_faculty' => $staff_faculty,
            'staff_specilay' => $staff_specilay,
            'staff_phone' => $staff_phone,
            'staff_student_card' => $staff_student_card,
            'staff_type' => 1,
            'staff_soldier_type' => $staff_soldier_type
        ];
        if($staff_soldier_type == 1){ // 国防军事类
            $data['staff_assn_create_time'] = $staff_assn_create_time;
            $data['staff_retire_people'] = $staff_retire_people;
            $data['staff_other_people'] = $staff_other_people;
            $data['staff_activity'] = $staff_activity;
            $data['staff_politics'] = $staff_politics;
            $data['staff_old_troops'] = $staff_old_troops;
            $data['staff_assn_duty'] = $staff_assn_duty;
//            $data['staff_front_img'] = $staff_front_img;
//            $data['staff_back_img'] = $staff_back_img;
            $data['staff_user_card'] = $staff_user_card;
            $data['staff_sign'] = $staff_sign;
            $data['staff_sign_time'] = $staff_sign_time;
        }else{ // 非国防军事类
            $data['staff_birthday'] = $staff_birthday;
            $data['staff_grade'] = $staff_grade;
        }
        
        if($is_have){ // 如果有，则更新
            $data['staff_status'] = 1;
            $data['staff_reason'] = null;
            $res = Db::table('up_staff')
                ->where('staff_user_id', $user_id)
                ->where('staff_type', 1)
                ->where('staff_soldier_type', $staff_soldier_type)
                ->update($data);
        }else{
            $data['staff_insert_time'] = date('Y-m-d H:i:s');
            $data['staff_user_id'] = $user_id;
            $res = Db::table('up_staff')->insert($data);
        }
       
        if($res){
            Db::table('up_tishi_admin')->where('id', 1)->setField('mass', 2); // 用于后台展示小红点
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 申请信息展示
     * @return \think\response\Json
     */
    public function instructorShow(){
        extract(input());
        $where['staff_user_id'] = $user_id;
        $where['staff_soldier_type'] = $staff_soldier_type; // 1 军事类  2 非军事类
        $where['staff_type'] = 1;
        
        if($staff_soldier_type == 1){ // 军事类
            $field = 'staff_id, staff_school_id, sc_name, staff_assn_name, staff_assn_create_time, 
            staff_assn_address, staff_assn_up, staff_assn_teacher, staff_assn_logo, staff_assn_intro, 
            staff_retire_people, staff_other_people, staff_activity, staff_name, staff_gender, staff_politics, politics_name, 
            staff_faculty, staff_old_troops, staff_specilay, staff_assn_duty, staff_phone, staff_student_card, 
            staff_user_card, staff_sign, staff_sign_time, staff_reason, staff_status';
            $info = Db::table('up_staff')
                ->join('up_school', 'sc_id = staff_school_id', 'left')
                ->join('up_politics', 'politics_id = staff_politics', 'left')
                ->where($where)
                ->field($field)
                ->find();
            // 社团活动
            $staff_activity = trim($info['staff_activity'], ',');
            $activity = Db::table('up_assn_activity')
                ->where('activity_id', 'IN', $staff_activity)
                ->field('activity_id, activity_name')
                ->select();
            $info['activity'] = $activity;
        }else{
            $field = 'staff_id, staff_school_id, sc_name, staff_assn_name, staff_assn_address, 
            staff_assn_up, staff_assn_teacher, staff_assn_logo, staff_assn_intro, staff_name, 
            staff_gender, staff_birthday, staff_faculty, staff_grade, staff_specilay, 
            staff_phone, staff_student_card, staff_reason, staff_status';
            $info = Db::table('up_staff')
                ->join('up_school', 'sc_id = staff_school_id', 'left')
                ->where($where)
                ->field($field)
                ->find();
        }
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 认证种类和状态
     * @return \think\response\Json
     */
    public function instructorStatus(){
        extract(input());
        $status = Db::table('up_staff')
            ->where('staff_user_id', $user_id)
            ->where('staff_type', 1)
            ->field('staff_soldier_type, staff_status')
            ->select();
        if($status){
            return json(['status' => 200, 'msg' => $status]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /***
     * @return \think\response\Json
     * 获取所有社团数据---供pc端使用
     * type,page
     */
    public function pcAllAssnData()
    {
        header("Access-Control-Allow-Origin: *");
        extract(input());
        $where['assn_status'] = 1;
        $up = Db::table('up_assn')->alias('a')
            ->join('up_school s', 'a.assn_school = s.sc_id', 'left')
            ->where($where)
            ->field('assn_id,assn_name,assn_image,sc_name,assn_code,assn_type')
            ->order('assn_time ASC')
            ->page($page,12)
            ->select();
        
        $count = Db::table('up_assn')
            ->where($where)
            ->count();
        return json(['status'=>200,'msg'=>$up, 'count' => $count]);
    }
    
    /***
     * @return \think\response\Json
     * 获取更多社团数据---供pc端使用
     * type,page
     */
    public function pcMoreAssnData()
    {
        header("Access-Control-Allow-Origin: *");
        extract(input());
        $where['assn_status'] = 1;
        $up = Db::table('up_assn')->alias('a')
            ->join('up_school s', 'a.assn_school = s.sc_id', 'left')
            ->where($where)
            ->field('assn_id,assn_name,assn_image,sc_name,assn_code,assn_type')
            ->order('assn_time ASC')
            ->limit(($page-1)*12,12)
            ->select();
        return json(['status'=>200,'msg'=>$up]);
    }
    /***
     * @return \think\response\Json
     * 获取搜索结果---供pc端使用
     * type,page
     */
    public function pcGetSearchResult()
    {
        header("Access-Control-Allow-Origin: *");
        $keyword = $_POST['keyword'];
        $where['staff_user_card'] = $keyword;
        $where['staff_soldier_type'] = 1;
        $where['staff_status'] = 2;
        $base = Db::table('up_staff')
            ->where($where)
            ->field('staff_user_id,staff_member_code')
            ->find();
        $uid = $base['staff_user_id'];
        if($uid){
            $aids = Db::table('up_assn_member')
                ->where('am_user_id',$uid)
                ->column('am_assn_id');
            if($aids){
                $res = Db::table('up_assn')->alias('a')
                    ->join('up_school s', 'a.assn_school = s.sc_id', 'left')
                    ->where('assn_id','in',$aids)
                    ->where('assn_type',1)
                    ->field('assn_id,assn_name,assn_image,sc_name,assn_code,assn_type')
                    ->find();
                if($res){
                    $res['assn_code'] = $base['staff_member_code'];
                    return json(['status'=>200,'msg'=>$res]);
                }else{
                    return json(['status'=>0,'msg'=>'没有搜索结果']);
                }
            }else{
                return json(['status'=>0,'msg'=>'没有搜索结果']);
            }
        }else{
            return json(['status'=>0,'msg'=>'没有搜索结果']);
        }

    }
}